import Image

def img2four(image):
    width = 10
    height= 10
    left = 0
    upper = 0
    right= 10
    lower = 10

    c = 4
    while(c):
        box = (left, upper, right, lower)
        im = Image.open(image)
        region = im.crop(box)
        region.convert('L').save('crop_'+str(4-c)+'.bmp')
        left = left + 10
        right = right + 10
        c = c -1


def printPixel(image):
    img = Image.open(image)

    for y in range(0, 10):
        for x in range(0, 10):
            print img.getpixel((x,y))

def cross(color):
    if color != 238:
        return True;
    else:
        return False;

def recognize(image):
    bgcolor = 238
    img = Image.open(image)

    P = img.getpixel((1, 8))
    print P
    if cross(P):
        return 7
    
    P = img.getpixel((0, 0))
    if cross(P):
        return 5

    P= img.getpixel((2, 1))
    if cross(P):
         return 1
        
    P = img.getpixel((3, 1))
    if cross(P):
         return 4

    p = img.getpixel((1,1))
    if cross(P):
        return 6
    
    p= img.getpixel((1, 7))
    if cross(P):
         return 2

 
    p = img.getpixel((2,5)) 
    if cross(p):
         return 9 
 
    p = img.getpixel((5,4)) 
    if cross(p):
     # not 9, must be 0
         return 0 
     
    p = img.getpixel((1,4)) 
    if cross(p):
         return 8 
    else:
         return 3

def getCode(image):
    img2four(image)
    no = recognize('crop_0.bmp')
    n1 = recognize('crop_1.bmp')
    n2 = recognize('crop_2.bmp')
    n3 = recognize('crop_3.bmp')
    return str(no) + str(n1) + str(n2) + str(n3)
import os
if os.path.exists('crop_0,bmp'):
    os.remove('crop_0.bmp')

if os.path.exists('crop_1,bmp'):
    os.remove('crop_1.bmp')

if os.path.exists('crop_2,bmp'):
    os.remove('crop_2.bmp')
    
if os.path.exists('crop_3,bmp'):
    os.remove('crop_3.bmp')
    
print getCode('1908.jpg')
